package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.DevTaskBo;
import com.wande.dataplatform.domain.dto.ExecutionPlanDTO;
import com.wande.dataplatform.domain.vo.DevInstanceVO;
import com.wande.dataplatform.domain.vo.DevTaskVO;
import com.wande.dataplatform.service.IDataDevService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 数据开发控制器
 *
 * @author wande
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/dev")
public class DataDevController extends BaseController {

    private final IDataDevService dataDevService;

    /**
     * 查询数据开发任务列表
     */
    @SaCheckPermission("dataplatform:dev:list")
    @GetMapping("/tasks")
    public TableDataInfo<DevTaskVO> listTasks(DevTaskBo bo, PageQuery pageQuery) {
        return dataDevService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询数据开发任务详情
     */
    @SaCheckPermission("dataplatform:dev:query")
    @GetMapping("/tasks/{id}")
    public R<DevTaskVO> getTask(@PathVariable Long id) {
        return R.ok(dataDevService.queryById(id));
    }

    /**
     * 创建数据开发任务
     */
    @SaCheckPermission("dataplatform:dev:add")
    @Log(title = "数据开发", businessType = BusinessType.INSERT)
    @PostMapping("/tasks")
    public R<Long> createTask(@Valid @RequestBody DevTaskBo bo) {
        return R.ok(dataDevService.createTask(bo));
    }

    /**
     * 更新数据开发任务
     */
    @SaCheckPermission("dataplatform:dev:edit")
    @Log(title = "数据开发", businessType = BusinessType.UPDATE)
    @PutMapping("/tasks/{id}")
    public R<Void> updateTask(@PathVariable Long id, @Valid @RequestBody DevTaskBo bo) {
        bo.setId(id);
        dataDevService.updateTask(bo);
        return R.ok();
    }

    /**
     * 删除数据开发任务
     */
    @SaCheckPermission("dataplatform:dev:remove")
    @Log(title = "数据开发", businessType = BusinessType.DELETE)
    @DeleteMapping("/tasks/{id}")
    public R<Void> deleteTask(@PathVariable Long id) {
        dataDevService.deleteTask(id);
        return R.ok();
    }

    /**
     * 执行SQL任务
     */
    @SaCheckPermission("dataplatform:dev:execute")
    @Log(title = "数据开发", businessType = BusinessType.OTHER)
    @PostMapping("/tasks/{id}/execute-sql")
    public R<Map<String, Object>> executeSql(@PathVariable Long id, @RequestBody(required = false) Map<String, Object> params) {
        return R.ok(dataDevService.executeSql(id, params));
    }

    /**
     * 执行脚本任务
     */
    @SaCheckPermission("dataplatform:dev:execute")
    @Log(title = "数据开发", businessType = BusinessType.OTHER)
    @PostMapping("/tasks/{id}/execute-script")
    public R<Map<String, Object>> executeScript(@PathVariable Long id, @RequestBody(required = false) Map<String, Object> params) {
        return R.ok(dataDevService.executeScript(id, params));
    }

    /**
     * 停止任务执行
     */
    @SaCheckPermission("dataplatform:dev:execute")
    @Log(title = "数据开发", businessType = BusinessType.OTHER)
    @PostMapping("/instances/{instanceId}/stop")
    public R<Void> stopExecution(@PathVariable Long instanceId) {
        dataDevService.stopExecution(instanceId);
        return R.ok();
    }

    /**
     * 获取执行计划
     */
    @SaCheckPermission("dataplatform:dev:query")
    @GetMapping("/tasks/{id}/execution-plan")
    public R<ExecutionPlanDTO> getExecutionPlan(@PathVariable Long id) {
        return R.ok(dataDevService.getExecutionPlan(id));
    }

    /**
     * 查询任务实例列表
     */
    @SaCheckPermission("dataplatform:dev:list")
    @GetMapping("/tasks/{taskId}/instances")
    public TableDataInfo<DevInstanceVO> listInstances(@PathVariable Long taskId, PageQuery pageQuery) {
        return dataDevService.queryInstanceList(taskId, pageQuery);
    }

    /**
     * 查询实例详情
     */
    @SaCheckPermission("dataplatform:dev:query")
    @GetMapping("/instances/{instanceId}")
    public R<DevInstanceVO> getInstance(@PathVariable Long instanceId) {
        return R.ok(dataDevService.queryInstanceById(instanceId));
    }

    /**
     * 发布任务
     */
    @SaCheckPermission("dataplatform:dev:publish")
    @Log(title = "数据开发", businessType = BusinessType.UPDATE)
    @PostMapping("/tasks/{id}/publish")
    public R<Void> publishTask(@PathVariable Long id) {
        dataDevService.publishTask(id);
        return R.ok();
    }

    /**
     * 下线任务
     */
    @SaCheckPermission("dataplatform:dev:offline")
    @Log(title = "数据开发", businessType = BusinessType.UPDATE)
    @PostMapping("/tasks/{id}/offline")
    public R<Void> offlineTask(@PathVariable Long id) {
        dataDevService.offlineTask(id);
        return R.ok();
    }
}
